//
//  DestinationDetailsViewController.m
//  EDPS
//
//  Created by Neil on 15/11/25.
//  Copyright © 2015年 张维扬. All rights reserved.
//

#import "DestinationDetailsViewController.h"
#import "DestinationHeaderView.h"
#import "DestinationSecTableViewCell.h"
#import "ZWY_NetHandle.h"
#import "UIImageView+WebCache.h"
#import "DestinationSecPostTableViewCell.h"
#import "hottest_places.h"
#import "DestinationSecButtonCollectionCell.h"
#import "DestinationAlbumViewController.h"
#import "TourPlaceViewController.h"

@interface DestinationDetailsViewController ()<UITableViewDataSource, UITableViewDelegate,DestinationSecTableViewCellDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSDictionary *dataDic;
@property (nonatomic, retain) NSMutableArray *photoArr;
@property (nonatomic, retain) NSString *url;
@property (nonatomic, retain)  NSMutableArray *dataArr;

@end

@implementation DestinationDetailsViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = YES;
    [self getData];
    [self creatTableView];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}
#pragma mark - 获取数据
- (void)getData
{
    self.dataDic = [NSDictionary dictionary];
    self.photoArr = [NSMutableArray array];
    self.url = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/", self.type, self.numId];
     NSString *cooie = @"bt_devid=i_515811fee9a51ce8130a8cf8e901bd7000624b8a24bc78c4e99e3297954f54ccad970e30;";
    [ZWY_NetHandle getURL:self.url Cookie:cooie parameter:nil result:^(id result)
    {
        self.dataDic = result;
       self.photoArr = [hottest_places transformWithArray:result[@"hottest_sites"]];
       [self.tableView reloadData];
    }];
}
#pragma mark - 头部手势方法
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    DestinationAlbumViewController *albumVc = [[DestinationAlbumViewController alloc]init];
    albumVc.strUrl = self.url;
    [self.navigationController pushViewController:albumVc animated:YES];
}

#pragma mark - 返回方法
- (void)backAction
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark - 创建TableView
- (void)creatTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[DestinationSecTableViewCell class] forCellReuseIdentifier:@"TableCell"];
    [self.tableView registerClass:[DestinationSecPostTableViewCell class] forCellReuseIdentifier:@"TableCell1"];
    [self.view addSubview:self.tableView];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return  self.view.frame.size.height / 3;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    DestinationHeaderView *headerView = [[DestinationHeaderView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 3)];
    headerView.userInteractionEnabled = YES;
    headerView.backgroundColor = [UIColor grayColor];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    [headerView addGestureRecognizer:tap];
    
    if ([self.type integerValue] == 3)
    {
        headerView.titleLable.text = self.dataDic[@"name_zh"];
        headerView.titleLable.textColor = [UIColor whiteColor];
        headerView.likeLable.font = [UIFont systemFontOfSize:10];
        headerView.likeLable.textColor = [UIColor whiteColor];
        headerView.likeLable.text = [NSString stringWithFormat:@"%@去过 %@喜欢", self.dataDic[@"visited_count"], self.dataDic[@"wish_to_go_count"]];
        [headerView sd_setImageWithURL:[NSURL URLWithString:self.dataDic[@"hottest_places"][0][@"photo"]]];
    }
    if ([self.type integerValue] == 1)
    {
        headerView.titleLable.text = self.dataDic[@"name_orig"];
        headerView.titleLable.textColor = [UIColor whiteColor];
        headerView.likeLable.textColor = [UIColor whiteColor];
        headerView.likeLable.font = [UIFont systemFontOfSize:10];
        headerView.likeLable.text = [NSString stringWithFormat:@"%@去过 %@喜欢", self.dataDic[@"visited_count"], self.dataDic[@"wish_to_go_count"]];
        [headerView sd_setImageWithURL:[NSURL URLWithString:self.dataDic[@"hottest_places"][0][@"photo"]]];
    }
    if ([self.type integerValue] == 2)
    {
        headerView.titleLable.text = self.dataDic[@"name_zh"];
        headerView.titleLable.textColor = [UIColor whiteColor];
        headerView.likeLable.textColor = [UIColor whiteColor];
        headerView.likeLable.font = [UIFont systemFontOfSize:10];
        headerView.likeLable.text = [NSString stringWithFormat:@"%@去过 %@喜欢", self.dataDic[@"visited_count"], self.dataDic[@"wish_to_go_count"]];
        [headerView sd_setImageWithURL:[NSURL URLWithString:self.dataDic[@"hottest_places"][0][@"photo"]]];
    }
    
    
#pragma mark - 返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [backButton setBackgroundImage:[UIImage imageNamed:@"iconfont-fan"] forState:UIControlStateNormal];
    [headerView addSubview:backButton];
    
    backButton.translatesAutoresizingMaskIntoConstraints = NO;
    
    NSArray *height = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[backButton(40)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(backButton)];
    
    NSArray *width = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[backButton(40)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(backButton)];
    
    [headerView addConstraints:height];
    [headerView addConstraints:width];
    
    return headerView;
}
#pragma mark - cell赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 0)
    {
        DestinationSecPostTableViewCell *tableCell = [tableView dequeueReusableCellWithIdentifier:@"TableCell1"forIndexPath:indexPath];
        
        [tableCell.bestTourButton addTarget:self action:@selector(bestTourAction) forControlEvents:UIControlEventTouchUpInside];
        [tableCell.tourPlaceButton addTarget:self action:@selector(tourPlaceAction) forControlEvents:UIControlEventTouchUpInside];
        
        return tableCell;
    }
    else if (indexPath.row == 1)
    {
        DestinationSecTableViewCell *tableCell = [tableView dequeueReusableCellWithIdentifier:@"TableCell"forIndexPath:indexPath];
        if (self.photoArr.count != 0)
        {
            tableCell.photoArr = self.photoArr;
            tableCell.delegate = self;
        }
        tableCell.backgroundColor = [UIColor blueColor];
        tableCell.delegate = self;
        return tableCell;
    }
    else
    {
        return nil;
    }
}

#pragma mark - 旅行地点方法
- (void)tourPlaceAction
{
    TourPlaceViewController *tourPlace = [[TourPlaceViewController alloc]init];
    [self.navigationController pushViewController:tourPlace animated:YES];
    
    NSLog(@"旅行地点");
}

#pragma mark - 精品游记方法
- (void)bestTourAction
{
    NSLog(@"精品游记");
}


#pragma mark - cell大小
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        return 50;
    }
    else if (indexPath.row == 1)
    {
        return 500;
    }
    else
    {
        return 300;
    }
}

#pragma mark - cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

#pragma mark - cell点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)pasIndexPath:(NSInteger)indexPath
{
    
}

@end